package tuling.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import tuling.netty.codec.domain.CodecUser;
import tuling.netty.codec.utils.ProtostuffUtil;

/**
 * @author RenGaoshuai
 * @date 2023/12/4 10:24
 * @description
 */
public class CodecClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        CodecUser codecUser = (CodecUser)msg;
//        System.out.println("[ 客户端接收到的数据 ]"+codecUser);
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        System.out.println("[ 客户端接收到的数据 ]:"+ProtostuffUtil.deserializer(bytes,CodecUser.class));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
